﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;
using System.IO;

public partial class Admin_Module_Photo_Controls_PhotoAE : System.Web.UI.UserControl
{
	public Guid PhotoId { get; set; }
	public Guid AlbumId { get; set; }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (Request["PhotoId"] != null)
		{
			PhotoId = new Guid(Request["PhotoId"].ToString());
			btnUpdate.Text = "Cập nhật";
		}
		else PhotoId = Guid.Empty;
		if (Request["AlbumId"] != null && !Request["AlbumId"].Equals(Guid.Empty.ToString()))
			AlbumId = new Guid(Request["AlbumId"].ToString());
		else
			Response.Redirect("/Admin/Module/Album/AlbumList.aspx");
		if (!IsPostBack)
		{
			LoadData();
		}

	}
	private void LoadData()
	{
		TblPhoto item = TblPhotoRepository.CreateInstant().SelectOne(PhotoId);
		if (item != null)
		{
			txtName.Text = item.Name;
			//cbIsVisible.Checked = item.IsVisible;
			if (!item.ImageUrl.Equals(string.Empty))
				Image1.ImageUrl = item.ImageUrl;
			else Image1.Visible = false;
			lblCreatedBy.Text = item.CreatedBy;
			lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
			lblUpdatedBy.Text = item.UpdatedBy;
			lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
		}
	}
	private TblPhoto GetTblPhoto()
	{
		TblAlbum _TblAlbum = TblAlbumRepository.CreateInstant().SelectOne(AlbumId);
		TblPhoto obj = new TblPhotoRepository().SelectOne(PhotoId);
		if (obj == null)
		{
			obj = new TblPhoto();
			obj.Id = Guid.NewGuid();
			obj.AlbumId = AlbumId;
			//obj.TextId = new TblPhotoRepository().GetUniqueTextIdFromUnicodeText(txtName.Text, 250);
			obj.TextId = "";
			obj.CreatedBy = HttpContext.Current.User.Identity.Name;
			obj.CreatedDate = DateTime.Now;
			obj.ImageUrl = "";
		}
		obj.Name = txtName.Text;
		string strSaveImage = SaveImage(FileUpload1.PostedFile, _TblAlbum.TextId);
		if (!strSaveImage.Equals(string.Empty))
		{
			obj.ImageUrl = strSaveImage;

		}
		obj.IsVisible = true;
		obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
		obj.UpdatedDate = DateTime.Now;
		return obj;
	}


	protected void btnUpdate_Click(object sender, EventArgs e)
	{
		if (PhotoId.Equals(Guid.Empty))
			new TblPhotoRepository().Insert(GetTblPhoto());
		else
			new TblPhotoRepository().Update(GetTblPhoto());
		TblAlbum abum = TblAlbumRepository.CreateInstant().SelectOne(AlbumId);
		CreatXML(abum.TextId);
		//Response.Redirect("PhotoList.aspx?AlbumId=" + AlbumId);
	}
	string SaveImage(HttpPostedFile file, string TextId)
	{
		string result = string.Empty;
		try
		{
			if (!file.FileName.Equals(string.Empty))
			{
				string FolderName = "userfiles/album/" + TextId;
				string FileName = DateTime.Now.ToString("ddMMyyyyhhmmss-") + TextId;
				string physicalFolderFullPath = HttpContext.Current.Server.MapPath("/" + FolderName);
				string _ext = "." + file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);

				if (!System.IO.Directory.Exists(physicalFolderFullPath)) System.IO.Directory.CreateDirectory(physicalFolderFullPath); // create image folder if not existed
				try
				{
					System.IO.File.Delete(physicalFolderFullPath + "/" + FileName + _ext);
				}
				catch { }

				file.SaveAs(physicalFolderFullPath + "/" + FileName + _ext);
				result = "/" + FolderName + "/" + FileName + _ext;
			}
		}
		catch (Exception ex)
		{
			throw new Exception("Error Upload Image " + ex);
		}
		return result;
	}
	void CreatXML(string AlbumTextId)
	{
		string result = string.Empty;
		HashSet<string> hs = new HashSet<string>();
		hs.Add("CreatedDate ASC");
		IList<TblPhoto> ls = TblPhotoRepository.CreateInstant().SelectListItem(null, AlbumId, "", "", null, hs);
		result = @"<news w=""67"" h=""44"" spacing =""5"" effect=""slideright"" duration=""3000"">";
		foreach (TblPhoto item in ls)
		{
			result += "\n<item thumb=\"" + item.ImageUrl + "?w=300&amp;c=1\" image=\"" + item.ImageUrl + "?w=650&amp;c=1\" title=\"" + item.Name + "\" description=\"\" link=\"" + item.ImageUrl + "?w=800&amp;c=1\"/>";
		}
		result += "\n</news>";
		StreamWriter sw;
		try
		{
			File.Delete(Server.MapPath("/userfiles/xmlfile/" + AlbumTextId + ".xml"));
		}
		catch { }
		sw = File.CreateText(Server.MapPath("/userfiles/xmlfile/" + AlbumTextId + ".xml"));
		sw.WriteLine(result);
		sw.Close();
	}
}
